<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/11
 * Time: 15:50
 */
namespace app\admin\controller;
use think\Db;
class Product extends Base
{
    /*
     * 首页
     */
    public function index()
    {
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['ag_name|name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('admin_goods g')
                ->join('admin_good_type t','g.ag_type = t.id','left')
                ->where($map)
                ->count();//计算总页面
            $lists = Db::name('admin_goods g')
                ->join('admin_good_type t','g.ag_type = t.id','left')
                ->where($map)
                ->field('ag_id,ag_name,ag_thumb,ag_money,ag_status,ag_jifen,name')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('product/index');
    }

    /*
     * 添加商品
     */
    public function productAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'ag_name'=>$ag_name,
                'ag_thumb'=>$ag_thumb,
                'ag_money'=>$ag_money,
                'ag_jifen'=>$ag_jifen,
                'ag_type'=>$ag_type,
                'ag_status'=>$ag_status,
                'ag_create_time'=>$time,
                'ag_update_time'=>$time
            ];
            $res = Db::name('admin_goods')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $cate = Db::name('admin_good_type')->field('id,name')->select();
        return $this->fetch('product/product_add',['cate'=>$cate]);
    }

    /*
     * 修改状态
     */
    public function changeStatus(){
        extract(input());
        $res = Db::name('admin_goods')->where('ag_id',$id)->setField ('ag_status',$status);
        if($res){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>100,'msg'=>'修改失败']);
        }
    }

    /*
     * 修改商品
     */
    public function productEdit(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'ag_name'=>$ag_name,
                'ag_thumb'=>$ag_thumb,
                'ag_money'=>$ag_money,
                'ag_jifen'=>$ag_jifen,
                'ag_type'=>$ag_type,
                'ag_update_time'=>$time
            ];
            $res = Db::name('admin_goods')->where('ag_id',input('ag_id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('admin_goods')->where('ag_id',input('id'))->field('ag_id,ag_name,ag_thumb,ag_money,ag_jifen,ag_type')->find();
        $cate = Db::name('admin_good_type')->field('id,name')->select();
        return $this->fetch('product/product_edit',['data'=>$data,'cate'=>$cate]);
    }

    /*
     * 删除商品
     */
    public function productDel(){
        $res = Db::name("admin_goods")->where('ag_id',input('id'))->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    /*
     * 分类
     */
    public function cate(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('admin_good_type')->where($map)->count();//计算总页面
            $lists = Db::name('admin_good_type')
                ->where($map)
                ->field('id,name,create_time,update_time')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('product/cate');
    }

    /*
     * 添加分类
     */
    public function cateAdd(){
        if(request()->isPost ()){
            extract(input());
            $param['name'] = $title;
            $time = date("Y-m-d H:i:s");
            $param['create_time'] = $time;
            $param['update_time'] = $time;
            $res = Db::name('admin_good_type')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('product/cate_add');
    }

    /*
     * 修改分类
     */
    public function cateEdit(){
        if(request()->isPost ()){
            extract(input());
            $param['name'] = $title;
            $param['update_time'] = date("Y-m-d H:i:s");
            $res = Db::name('admin_good_type')->where('id',input('id'))->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('admin_good_type')->where('id',input('id'))->find();
        return $this->fetch('product/cate_edit',['data'=>$data]);
    }

    /*
     * 删除
     */
    public function cateDel(){
        $res = Db::name('admin_good_type')->where('id',input('id'))->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}